package com.ruoyi.basics.controller;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Hashtable;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.ruoyi.basics.dto.HouseholdRegisterDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basics.domain.User;
import com.ruoyi.basics.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 住户管理Controller
 *
 * @author ruoyi
 * @date 2022-10-06
 */
@RestController
@RequestMapping("/basics/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * 查询住户管理列表
     */
    @PreAuthorize("@ss.hasPermi('basics:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user) {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 查询住户管理列表
     */
    @PreAuthorize("@ss.hasPermi('basics:user:bese')")
    @GetMapping("/QRlist")
    public void getCode1(User user) throws WriterException, IOException {

        //生成的二维码的路径
        //  String QR_CODE_IMAGE_PATH = "D:\\IdeaProjects\\ruiyi-ms\\target\\classes\\META-INF\\resources\\code.png";
        String QR_CODE_IMAGE_PATH = "D:\\home";
        //二维码中的内容
        String QR_CODE_TEXT = user.toString();
        //二维码图片的宽度
        int WIDTH = 300;
        //二维码图片的高度
        int HEIGHT = 300;
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//H最高容错等级
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = qrCodeWriter.encode(QR_CODE_TEXT, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
        Path path = FileSystems.getDefault().getPath(QR_CODE_IMAGE_PATH);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

    }

    /**
     * 导出住户管理列表
     */
    @PreAuthorize("@ss.hasPermi('basics:user:export')")
    @Log(title = "住户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "住户管理数据");
    }

    /**
     * 获取住户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basics:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(userService.selectUserById(id));
    }

    /**
     * 新增住户管理
     */
    @PreAuthorize("@ss.hasPermi('basics:user:add')")
    @Log(title = "住户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改住户管理
     */
    @PreAuthorize("@ss.hasPermi('basics:user:edit')")
    @Log(title = "住户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除住户管理
     */
    @PreAuthorize("@ss.hasPermi('basics:user:remove')")
    @Log(title = "住户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.deleteUserByIds(ids));
    }


    // 手机注册
    @PostMapping("/register/phone")
    public AjaxResult phoneRegister(@RequestBody HouseholdRegisterDto householdRegisterDto) {
        try {
            userService.phoneRegister(householdRegisterDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult();
        }
    }
}
